package com.gargi.crimepatrol.network;

import java.util.Vector;

import com.gargi.crimpatrol.utils.Constant;

public class CrimePatrolOperation extends NetworkOperation {
	
	public interface CrimePatrolDataListener {
		
		public void onCrimeResponseReceived(Vector responseVector);
		
		public void onErrorReceived();
	}
	
	CrimePatrolDataListener m_dataListner;
	String m_feedurl;
	
	public CrimePatrolOperation(CrimePatrolDataListener dataListner) {
		m_dataListner = dataListner;
		m_feedurl = Constant.BASE_URL +
					Constant.CRIME_PATROL_PLAYLIST_ID +
					Constant.YOUTUBE_API_VERSION;
	}

	public void start() {
		new Thread() {
			public void run() {
				makeNetworkRequest();
			}
		}.start();
		
	}
	
	private void makeNetworkRequest() {
		Network.queue(this);
	}

	public String getUrl() {
		return m_feedurl;
	}
	
	public final int getMethod() {
		return METHOD_GET;
	}
		
	public final void networkHttpGetResponse(byte[] data) {
		String response = new String(data);
		if(response.length() > 0 ) {
			parseCrimePatrolResponse(response);	
		}else {
			m_dataListner.onErrorReceived();
		}
	}
	
	private void parseCrimePatrolResponse(String response) {
		try {
			CrimePatrolParser parser = new CrimePatrolParser();
			parse(response, parser);
			m_dataListner.onCrimeResponseReceived(parser.getCrimePatrolData());
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	
}
